package com.autumn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StockController {
    @Autowired
    private StringRedisTemplate template;

    @RequestMapping("/reduce_stock")
    public String reduceStock() {
        //单体使用reentrantLock，性能优于synchronized；分布式使用分布式锁
        //查库存   库存超卖
        int stock = Integer.parseInt(template.opsForValue().get("stock"));
        if (stock > 0) {
            //扣减库存
            stock--;
            template.opsForValue().set("stock", stock + "");
            System.out.println(">>>>>>>>>> 扣减成功，库存stock：" + stock);
        } else {
            System.out.println("扣减失败！库存不足！");
        }
        return "end";
    }
}
